<?php
namespace app\admin\model;

use think\Model;
use think\Db;
class AttachmentModel extends Model
{
    // 确定链接表名
    protected $name = 'attachment';

    /**
	 * [获取附件的路径]
     */
	public function getAttachmentPath($id=null,$which='url'){
		return $this->where('id',$id)->where('status',1)->value($which);
	}


	/**
	 * [获取信息]
	 */
	public function getInfoByWhere($where='',$is_page=false,$offset=0,$limit=20,$order=''){
		if($is_page){
			return $this->where($where)->Where($this->whereCansee())->limit($offset, $limit)->order($order)->select();
		}else{
			return $this->where($where)->Where($this->whereCansee())->order($order)->select();
		}
	}

	/**
	 * [设置附件状态]
	 */
	public function setstatus($idArr){
		$this::startTrans();
		try{
			foreach($idArr as $v){
				if($this->where('id',$v)->value('status') == 1){
					$flag = 0;
				}else{
					$flag = 1;
				}
				$this->where('id',$v)->update(['status'=>$flag]);
			}
			$this::commit();
			return ['code'=>1,'msg'=>'状态设置成功','data'=>''];
		}catch(Exception $e){
			$this::rollback();
			return ['code'=>0,'msg'=>'状态设置失败：'.$e->getMessage(),'data'=>''];
		}
	}
}